﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using DevExpress.Web.ASPxGridView;

public partial class AnalysisReport_ConsumerSpendingHabits : Frame.Pages.BasePage
{
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        Frame.Global.GlobalFunctions.SetGridView(this.gridView, false);
        BackLink.NavigateUrl = (Frame.Pages.RefrenceContext<DAL.BI_Consumer>.Current != null ? Frame.Pages.RefrenceContext<DAL.BI_Consumer>.Current.BackUrl : "");
        if (!IsPostBack && !IsCallback)
        {
            datStart.Date = DateTime.Today;
            datEnd.Date = DateTime.Today;

            Dictionary<string, SqlParameter> ParamDic = new Dictionary<string, SqlParameter>();
            ParamDic.Add("ConsumerID", new SqlParameter("@ConsumerID", Frame.Pages.RefrenceContext<DAL.BI_Consumer>.Current != null ? Frame.Pages.RefrenceContext<DAL.BI_Consumer>.Current.Model.ConsumerID : -1));
            ParamDic.Add("BeginDate", new SqlParameter("@BeginDate", DateTime.MinValue.AddYears(1900)));
            ParamDic.Add("EndDate", new SqlParameter("@EndDate", DateTime.MaxValue));
            Session["sqlstr"] = tabelsql;
            Session["TablesqlParams"] = ParamDic;
        }
        if (IsCallback)
        {
            if (this.datStart.Date == null)
            {
                this.datStart.Date = DateTime.Today;
            }
            if (this.datEnd.Date == null)
            {
                this.datEnd.Date = DateTime.Today;
            }
        }

        DDataBind();
        InitDetailpanel();
    }

    DataTable table;
    public DataTable Table
    {
        get
        {
            if (Session["sqlstr"] != null && Session["TablesqlParams"] != null)
            {
                table = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(Session["sqlstr"].ToString(), (Session["TablesqlParams"] as Dictionary<string, SqlParameter>).Values.ToArray());
            }
            return table;
        }
    }

    const string tabelsql = @"select       '排名'=0,
                                           b.TableName as 餐桌名称,
	                                       c.AreaName as 区域名称,
                                           COUNT(a.Table_ID) as 总次数,
                                           b.PCount as 人数
                                    from   dbo.BLI_ConsumerHisDetail a ,
                                           dbo.BI_Tables b,
                                           dbo.BI_Area c
                                    where  a.Table_ID=b.TableID and
										   b.Area_ID=c.AreaID and
                                           a.BookNo in (select BookNo 
                                                        from   dbo.BLI_ConsumerHis 
                                                        where  Consumer_ID=@ConsumerID and
                                                               ConsumerDate >=@BeginDate and
                                                               ConsumerDate <@EndDate)      
                                    group by a.Table_ID,
                                             b.PCount,
                                             b.TableName,
                                             c.AreaName
                                    order by 总次数 desc";

    void DDataBind()
    {
        if (Session["sqlstr"] != null && Session["TablesqlParams"] != null)
        {
            gridView.Columns.Clear();
            //可自动生成列
            gridView.AutoGenerateColumns = true;

            //GridViewDataTextColumn col = new GridViewDataTextColumn();
            //col.Caption = "排名";
            //col.VisibleIndex = 0;
            //col.EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.False;
            //ITemplate 
            //col.DataItemTemplate = 
            //gridView.Columns.Add(col);

            //排名
            int i = 1;
            DataTable ct = Table;
            if (ct != null)
            {
                foreach (DataRow row in ct.Rows)
                {
                    row["排名"] = i;
                    i++;
                }
            }
            gridView.DataSource = ct;
            gridView.DataBind();
        }
    }

    void InitDetailpanel()
    {
        decimal min = 0;
        decimal max = 0;
        decimal total = 0;
        int cviews = 0;
        int table = 0;
        using (SqlDataReader dr = Frame.Global.GlobalSqlHelper.GetSQLDataReader("select MIN(money) as [min],MAX(Money) as [max],SUM(Money) as total,COUNT(Money) as [views],SUM(TableCount) as [table] from dbo.BLI_ConsumerHis where Consumer_ID=@ConsumerID and ConsumerDate>=@BeginDate and ConsumerDate<@EndDate", (Session["TablesqlParams"] as Dictionary<string, SqlParameter>).Values.ToArray()))
        {
            if (dr.Read())
            {
                min = dr["min"] != DBNull.Value ? (decimal)dr["min"] : -1;
                max = dr["max"] != DBNull.Value ? (decimal)dr["max"] : -1;
                total = dr["total"] != DBNull.Value ? (decimal)dr["total"] : -1;
                cviews = (int)dr["views"];
                table = dr["table"] != DBNull.Value ? (int)dr["table"] : -1;
            }

        }

        View_Min.Text = min == -1 ? "无" : min.ToString();
        View_Max.Text = max == -1 ? "无" : max.ToString();
        View_Total.Text = total == -1 ? "无" : total.ToString();
        View_CViews.Text = cviews == -1 ? "无" : cviews.ToString();
        if (total != -1 && cviews != -1)
        {
            View_CAve.Text = (total / cviews).ToString();
        }
        else
        {
            View_CAve.Text = "无";
        }
        View_Table.Text = table == -1 ? "无" : table.ToString();
        if (total != -1 && table != -1)
        {
            View_TableAve.Text = (total / table).ToString();
        }
        else
        {
            View_TableAve.Text = "无";
        }

    }

    protected void gridView_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
    {
        switch (int.Parse(e.Parameters))
        {
            case 0:
                {
                    Session["sqlstr"] = tabelsql;
                }
                break;
            case 1:
                {
                    string dishsql = @"select  '排名'=0,
                                              b.DishName as 菜品名称,
                                              c.TypeName as 类别, 
                                              SUM(a.[Count]) as 消费数量, 
                                              a.Dish_ID as 菜品编号,
                                              b.Value as 单价
                                       from   dbo.BLI_ConsumerHisList a,
                                              dbo.BI_Dish b,
                                              dbo.BI_DishType c 
                                       where  a.Dish_ID=b.DishID and 
                                              b.Type_ID=c.DishTypeID and
                                              a.ConsumeNo in (select ConsumNo 
                                                              from   dbo.BLI_ConsumerHisDetail 
                                                              where  BookNo 
                                                              in (select BookNo 
                                                                  from   dbo.BLI_ConsumerHis 
                                                                  where  Consumer_ID=@ConsumerID and
                                                                         ConsumerDate >=@BeginDate and
                                                                         ConsumerDate <@EndDate) )
                                       group by a.Dish_ID,
                                                b.DishName,
                                                b.Value,
                                                c.TypeName
                                       order by 消费数量 desc";
                    Session["sqlstr"] = dishsql;
                }
                break;
            case 2:
                {
                    string timesql = @"select  '排名'=0,
                                              b.PeriodName as 时段名称,
	                                          COUNT(a.BookNo) as 就餐次数
                                       from   dbo.BLI_ConsumerHis a,
                                              dbo.BI_ConsumePeriod b
                                       where  a.Period_ID = b.PeriodID and
                                              a.Consumer_ID=@ConsumerID and
                                              a.ConsumerDate >=@BeginDate and
                                              a.ConsumerDate <@EndDate
                                       group by b.PeriodName
                                       order by 就餐次数 desc";
                    Session["sqlstr"] = timesql;
                }
                break;
            default:
                break;
        }
        DDataBind();
    }

    protected void QueryCBPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        if (datStart.Date.Subtract(datEnd.Date).TotalDays > 0)
        {
            throw new Exception("结束时间必须大于开始时间！");
        }
        Dictionary<string, SqlParameter> ParamDic = Session["TablesqlParams"] as Dictionary<string, SqlParameter>;
        ParamDic["BeginDate"] = new SqlParameter("@BeginDate", datStart.Date);
        ParamDic["EndDate"] = new SqlParameter("@EndDate", datEnd.Date.AddHours(23).AddMinutes(59).AddSeconds(59));
        DDataBind();
        InitDetailpanel();
    }

    protected override string ActionID
    {
        get { return "0"; }
    }
}